<template>
  <div class="email-verification">
    <h2>{{setTitle}}</h2>
    <el-form label-position="left" label-width="200px" :model="fundForm">
      <el-form-item :label="fundPassword">
        <el-input v-model="fundForm.fundpassword" type="password" :placeholder="'Enter '+fundPassword"  @input="testAndVerify"></el-input>
      </el-form-item>
      <el-form-item label="Confirm Fund Password">
        <el-input v-model="fundForm.confirmfundpassword" type="password" placeholder="Confirm Fund Password"  @input="testAndVerify"></el-input>
      </el-form-item>
      <el-form-item label="SMS Code">
        <el-input v-model="fundForm.sms_code" class="email-code" placeholder="Enter Email Code"  @input="testAndVerify"></el-input>
        <el-button type="warning" class="get-code" @click="getEnailCode('newCode')" ref="newCode" plain>Get Code</el-button>
      </el-form-item>
      <el-form-item>
        <el-button :type="primary" :disabled="confirmdb" class="email-confirm" @click="changFund">CONFIRM</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import { resend } from "@/filter/index";
import { verCode } from "@/api/userLogin";
import { setFundPassword, forgotFundPassword } from "@/api/kycAttest";
export default {
  data() {
    return {
      setFun: 0,
      setTitle: 'Fund Password',
      fundPassword: 'Fund Password',
      fundForm: {
        fundpassword: "",
        confirmfundpassword: "",
        sms_code: ""
      },
      confirmdb: true,
      primary: 'info'
    };
  },
  computed: {
    ...mapGetters({
      someinfo: "someinfo"
    })
  },
  created() {
    this.$nextTick(() => {
      this.setFun = this.someinfo.fund_password_isset
      if (this.setFun) {
        this.setTitle = "Forgot Fund Password"
        this.fundPassword = "New Fund Password"
      } else {
        this.setTitle = "Fund Password"
        this.fundPassword = "Fund Password"
      }
    })
  },
  methods: {
    /**
     * [getEnailCode description]
     * 获取验证码
     * ---------------------------------
     * @Author   LLP
     * @DateTime 2019-12-25T14:53:12+0800
     * @return   {[type]}                 [description]
     */
    async getEnailCode(code) {
      let param = null
      resend(this.$refs[code])
      if (this.setFun) {
        param = {
          phone: this.someinfo.phone,
          type: '08'
        }
      } else {
        param = {
          phone: this.someinfo.phone,
          type: '06'
        }
      }
      let { data } = await verCode(param)
      if (data === null) {
        this.$message({
          message: 'The verification code has been sent, please check.',
          type: 'success'
        });
      }
    },
    /**
     * [changFund description]
     * 设置-忘记资金密码
     * ---------------------------------
     * @Author   LLP
     * @DateTime 2019-12-25T15:55:37+0800
     * @return   {[type]}                 [description]
     */
    async changFund() {
      if (this.setFun) {
        let param = {
          new_fund_password: this.fundForm.fundpassword,
          confirm_new_fund_password: this.fundForm.confirmfundpassword,
          sms_code: this.fundForm.sms_code
        }
        let { data } = await forgotFundPassword(param)
        if ( data === null ) {
          this.$message({
            message: 'The fund password is set successfully.',
            type: 'success'
          })
          this.$router.push({ path: '/account' })
        }
      } else {
        let param = {
          ...this.fundForm
        }
        let { data } = await setFundPassword(param)
        if (data === null) {
          this.$message({
            message: 'The fund password is set successfully.',
            type: 'success'
          })
          this.$store.dispatch('setsomeinfo')
          this.$router.push({ path: '/account' })
        }
      }
    },
    /**
     * [testAndVerify description]
     * 按钮是否可操作
     * ----------------------------------
     * @Author   LLP
     * @DateTime 2019-12-25T14:56:11+0800
     * @return   {[type]}                 [description]
     */
    testAndVerify() {
      let from = this.fundForm
      if (from.fundpassword !="" && (from.confirmfundpassword !="" && from.sms_code)) {
        this.confirmdb = false
        this.primary = 'primary'
      } else {
        this.confirmdb = true
        this.primary = 'info'
      }
    }
  }
};
</script>
<style lang="less" scoped>
.email-verification {
  height: 100vh;
  h2 {
    font-size: 26px;
    display: inline-block;
    width: 100%;
    padding-bottom: 20px;
    border-bottom: 2px solid #edf0f4;
  }
  .el-form {
    padding: 45px 160px 0 0;
    .email-code {
      width: 450px;
      margin-right: 38px;
      display: inline-block;
    }
    .get-code {
      width: 105px;
    }
    .email-confirm {
      width: 100%;
    }
  }
}
</style>